#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include "RingQueue.hpp"
#include "Functions.hpp"

#define CNUM 3
#define PNUM 2

void *consumer(void *args)
{
    Ring_Queue<Task> *rq = (Ring_Queue<Task> *)args;
    // 使用数据
    while(true)
    {
        // if(rq->size() == 5)
            // std::cout << "||||||||||||||||" << std::endl;
        Task x;
        rq->pop(&x);
        // std::cout << "消费一个数据：" << x << std::endl;
        std::cout << x._x << name_v[x._type] << x._y << "=" << x() << std::endl;
        usleep(10000); // 消费数据
    }
    return nullptr;
}

void *producter(void *args)
{
    Ring_Queue<Task> *rq = (Ring_Queue<Task> *)args;
    // 生产数据
    while (true)
    {
        // if(rq->size() == 5)
            // std::cout << "||||||||||||||||" << std::endl;
        usleep(10000); // 生产数据
        // int x = rand() % 100 + 1;
        int x = rand() % 100 + 1;
        usleep(1000);
        int y = rand() % 100 + 1;
        int type = rand() % 4;
        // std::cout << "type:" << type << std::endl;
        rq->push(Task(x, y, type, func_v[type]));
        std::cout << x << name_v[type] << y << "=?" << std::endl;
        // std::cout << "生产一个数据：" << x << std::endl;
    }
    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));
    lode_func();
    lode_name();
    pthread_t c[CNUM];
    pthread_t p[PNUM];
    Ring_Queue<Task> *rq = new Ring_Queue<Task>();

    for (int i = 0; i < CNUM; ++i)
    {
        pthread_create(c + i, nullptr, consumer, (void *)rq);
    }
    for (int i = 0; i < PNUM; ++i)
    {
        pthread_create(c + i, nullptr, producter, (void *)rq);
    }

    for (int i = 0; i < CNUM; ++i)
    {
        pthread_join(c[i], nullptr);
    }
    for (int i = 0; i < PNUM; ++i)
    {
        pthread_join(p[i], nullptr);
    }

    return 0;
}